package com.zky.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.zky.service.UserService;
import com.zky.domain.dto.PageDto;
import com.zky.domain.po.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import com.zky.domain.po.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zky.domain.po.PageResult;

@RestController
@RequestMapping("/user")
@Api(tags = "User")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @ApiOperation("add User")
    public Result add(@RequestBody User entity) {

        return Result.ok(userService.save(entity));

    }

    @DeleteMapping("/{id}")
    @ApiOperation("delete User")
    public Result delete(@PathVariable Integer id) {

        return Result.ok(userService.removeById(id));

    }

    @DeleteMapping()
    @ApiOperation("delete User by ids")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        return Result.ok(userService.removeByIds(ids));

    }

    @PutMapping
    @ApiOperation("update User")
    public Result update(@RequestBody User entity) {

        return Result.ok(userService.updateById(entity));

    }

    @GetMapping("/{id}")
    @ApiOperation("get User by id")
    public Result get(@PathVariable Integer id) {

        return Result.ok(userService.getById(id));

    }

    @GetMapping
    @ApiOperation("get User by pageDto")
    public Result list(PageDto pageDto) {
        Page<User>page = new Page<>(pageDto.getPageNo(),pageDto.getPageSize());
        Page<User>page1 = userService.page(page);
        return PageResult.of(page1.getRecords(),page1.getTotal());

    }

    @GetMapping("list")
    @ApiOperation("get All")
    public Result findAll() {
    List<User> list = userService.list();
        return Result.ok(list);

    }


}
